// Copyright©,2020-2025
// Author: caocheng
// Version: 1.0.0
// Date: 2021/2/25 14:41
// Description：
package SummerWeb

// Router 路由分组
type Router struct {
	UrlRegister

	mapping  WebMapping
	basePath string
	filters  []Filter
}

// NewRouter Router 的构造函数，不依赖具体的 WebMapping 对象
func NewRouter(basePath string, filters ...Filter) *Router {
	return routerWithMapping(NewDefaultWebMapping(), basePath, filters)
}

// routerWithMapping Router 的构造函数，依赖具体的 WebMapping 对象
func routerWithMapping(mapping WebMapping, basePath string, filters []Filter) *Router {
	r := &Router{}
	r.filters = filters
	r.mapping = mapping
	r.basePath = basePath
	r.UrlRegister = &defaultUrlRegister{request: r.request}
	return r
}

func (r *Router) request(method uint32, path string, fn Handler, filters []Filter) *Mapper {
	filters = append(r.filters, filters...)
	return r.mapping.Request(method, r.basePath+path, fn, filters...)
}

// Route 返回和 Mapping 绑定的路由分组
func (r *Router) Route(basePath string, filters ...Filter) *Router {
	filters = append(r.filters, filters...)
	return routerWithMapping(r.mapping, r.basePath+basePath, filters)
}